#include <crt_util.h>
#include "test.h"

int time_handler(evevent res, evoper_t* oper, evoper_t* oper_s, evevent event)
{
    evloop_t* evloop;
    crt_msec opermsec = 0;
    evoper_get_msec(oper, &opermsec);

    crt_msec clockmsec = 0;
    evoper_get_evloop(oper, &evloop);
    evloop_get_clock(evloop, &clockmsec);

    if (event == op_ev_timer) {
        if (res == op_ev_success) {
            logi("timer %ld==%ld", clockmsec, opermsec);
            int* id = NULL;
            evoper_get_ctx(oper, (void**)&id);
            // sleep_time(4);
        } else {
            logi("timer out %ld==%ld", clockmsec, opermsec);
            int* id = NULL;
            evoper_get_ctx(oper, (void**)&id);
        }
    }

    return S_SUCCESS;
}
void test_timer(evloop_t* loop)
{
    evoper_t* timer1;
    evoper_t* timer2;
    evoper_t* timer3;
    evoper_alloc(&timer1, NULL, oper_alloc);
    evoper_alloc(&timer2, NULL, oper_alloc);
    evoper_alloc(&timer3, NULL, oper_alloc);

    int id1 = 1;
    int id2 = 2;
    int id3 = 3;

    struct timeval tv;
    tv.tv_sec = 10;
    tv.tv_usec = 0;
    evoper_bind_ctx(timer1, &id1);
    evoper_bind_handler(timer1, time_handler);
    evloop_add_timer(loop, timer1, &tv);

    tv.tv_sec = 12;
    tv.tv_usec = 0;
    evoper_bind_ctx(timer2, &id2);
    evoper_bind_handler(timer2, time_handler);
    evloop_add_timer(loop, timer2, &tv);

    tv.tv_sec = 14;
    tv.tv_usec = 0;
    evoper_bind_ctx(timer3, &id3);
    evoper_bind_handler(timer3, time_handler);
    evloop_add_timer(loop, timer3, &tv);
}